<?php
/**
 * Dependency abstract file exporter
 *
 * PHP version 5
 *
 * @category  W
 * @package   Services
 * @author    Olivier Hoareau <olivier@phppro.fr>
 * @copyright 2009 PHPPRO http://www.phppro.fr
 * @license   http://dev.phppro.fr/license PHPPRO
 * @version   SVN: $Id:$
 * @link      http://dev.phppro.fr/projects/wdependency
 */
require_once 'W/Dependency/Exporter/Abstract.php';
/**
 * Dependency abstract file exporter
 *
 * @category  W
 * @package   Services
 * @author    Olivier Hoareau <olivier@phppro.fr>
 * @copyright 2009 PHPPRO http://www.phppro.fr
 * @license   http://dev.phppro.fr/license PHPPRO
 * @version   SVN: $Id$
 * @link      http://dev.phppro.fr/projects/wdependency
 */
abstract class W_Dependency_Exporter_File_Abstract extends W_Dependency_Exporter_Abstract
{
    private $outputFile;
    private $template;
    private $options;

    public function __construct()
    {
        parent::__construct();
        $this->outputFile = null;
        $this->template = null;
        $this->onlyPatterns = array();
    }
    public function configure($options=null)
    {
        parent::configure($options);
        if (true === isset($options['template'])) {
            $this->template = $options['template'];
        }
        if (true === isset($options['output'])) {
            $this->setOutputFile($options['output']);
        }
        $this->options = $options;
        return $this;
    }
    protected function generateFile($outputFile,$viewTemplate,$data,$root)
    {
        $options = $this->options;
        ob_start();
        if (false === W_W::fs()->isExistingFile($viewTemplate)) {
            throw new RuntimeException("Template '$viewTemplate' does not exist");
        }
        include $viewTemplate;
        $content = ob_get_contents();
        ob_end_clean();
        file_put_contents($outputFile,$content);
        return $this;
    }

    public function getOutputFile()
    {
        return $this->outputFile;
    }
    public function setOutputFile($outputFile)
    {
        $this->outputFile = $outputFile;
        return $this;
    }
    /**
	 * Exports the dependencies
	 *
	 * @param array $data the dependencies
	 *
	 * @return W_Dependency_Exporter_Interface
	 *
	 * @throws RuntimeException if an error occured
     */
    public function export($data)
    {
        $root = $data['root'];
        $data = $data['data'];
        $outputFile = $this->getOutputFile();
        if (null === $this->template) {
            $this->template = dirname(__FILE__).'/resources/'.strtolower($this->getFormat()).'.phtml';
        }
        if (null !== $this->getKey()) {
            if (false === isset($data[$this->getKey()])) {
                $data[$this->getKey()] = array();
            }
            $data = $data[$this->getKey()];
        }
        if (null === $outputFile) {
            $outputFile = $this->getKey().'.'.$this->getFormat();
        }
        $this->generateFile($outputFile,$this->template,$data,$root);
        return $this;
    }
    public function __toString()
    {
        return (null !== $this->getKey() ? $this->getKey() : ''). ' in '.strtoupper($this->getFormat()).' format to '.$this->outputFile;
    }

}